<template>
	<view class="productTabbar">
		<view class="bottom">
			<view class="button button1">
				<text @click="toIndex">首页</text>
				<text @click="toCart">购物车</text>
			</view>
			<view class="button button2">
				<text @click="showPopup('add')">加购物车</text>
			</view>
			<view class="button button3">
				<text @click="showPopup('buy')">立即购买</text>
			</view>
			<view class="share" @click="share()">
				<text>分享</text>
			</view>
		</view>
		
	</view>
</template>
<script>
	import uniPopup from '../../../../components/uni-popup.vue'
	import uniNumberBox from '../../../../components/uni-number-box.vue'
	import Request from '../../../../common/request.js'
	import { mapActions } from 'vuex'
	export default {
		components: {
			uniPopup,
			uniNumberBox,
		},
		props:[
			'commodityId',
			'commodityInfo'
		],
		data(){
			return {
					specPopup: false,
					number:1,
					specList: [],
					commoditySpecList:[],
					selectId: [],
					selectValue: [],
					commoditySpecId:'',
					marketingPrice: 0, //规格默认价格
					inventoryAmount:0,  //规格默认库存
					integral:0,//规格默认积分
					bottomClickType:'',
			}
		},
		methods:{
			...mapActions(['orderConfirm']),
			//展示底部 popup
			showPopup(type) {
				this.specPopup = true;
				this.bottomClickType = type;
				this.getSpecData();
			},
			//统一的关闭popup方法
			hidePopup() {
				this.specPopup = false;
			},
			getSpecData(){
				Request.fetchPost('/fsbus/M2103',{commodityId: this.commodityId}).then(res=>{
					if(res.errorNo == 0){
						this.specList = res.results.specList;
						this.commoditySpecList = res.results.commoditySpecList;
						for(var i=0; i<this.specList.length;i++){
							this.selectId.push('');
							this.selectValue.push('');
						}
					}
					console.log(this.specList)
				}).catch(err=>{
				})
			},
			setSpecValue(i,id,value){
				this.$set(this.selectId,i,id);
				this.$set(this.selectValue,i,value)
				for(var i=0; i< this.commoditySpecList.length; i++) {
					if(JSON.stringify(this.commoditySpecList[i].specOptionIdList) == JSON.stringify(this.selectId)){
						this.commoditySpecId = this.commoditySpecList[i].commoditySpecId;
						this.marketingPrice = this.commoditySpecList[i].marketingPrice;
						this.inventoryAmount = this.commoditySpecList[i].inventoryAmount;
						this.integral = this.commoditySpecList[i].integral;
					}
				}
			},
			specConfirm(){
				if(this.commoditySpecId == ''){
					return false;
				}
				
				if(this.bottomClickType == 'add') {
					/* 加入购物车 */
					Request.fetchPost('/fsbus/M2209',{
						commoditySpecId: this.commoditySpecId,
						amount: this.number
					}).then(res=>{
						if(res.errorNo == 0){
							this.hidePopup();
							uni.showToast({
									title: '加入购物车成功',
									duration: 2000
							});
						}
					}).then(err=>{
					})
				}else if(this.bottomClickType == 'buy'){
					/* 购买*/
					let productList = [];
					var params = {};
					params.commodityId = this.commodityId;
					params.commoditySpecId = this.commoditySpecId;
					params.commoditySpecValue = this.selectValue.join(',');
					params.buyAmount = this.number;
					params.marketingPrice = this.marketingPrice;
					params.cartId = '';
					params.commodityName = this.commodityInfo.commodityName;
					params.commodityPicture = this.commodityInfo.commodityPicture;
					productList.push(params);
					/* 将数据存储到vuex，确认订单页面的时候取出 */
					this.orderConfirm(productList);
					uni.navigateTo({
							url: '/pages/product/orderConfirm/orderConfirm'
					});
				}
			},
			onNumberChange(value){
				this.number = value;
			},
			toIndex(){
				uni.redirectTo({
						url: '/pages/index/index/index'
				});
			},
			toCart(){
				uni.redirectTo({
					url: '/pages/cart/cart'
				})
			},
			share(){
				this.$emit('share')
			},
		}
	}
</script>
<style>

</style>